<?php

class Admin_GeneralController extends Zend_Controller_Action
{
	// Do some checks here (including authorization)
	public function init()
	{
		parent::init();

		$sessAccount = Account_Model_Session::getAccount();
		if ($sessAccount === NULL) {
			$this->_redirect('/');
		}
		$gOpts = Kernel::getInstance()->getOptions();
		if (is_array($gOpts['site']) && is_array($gOpts['site']['administrator'])
			&& isset($gOpts['site']['administrator']['idname'])) {
			if ($sessAccount->getIdName() != $gOpts['site']['administrator']['idname']) {
				$this->_redirect('/');
			}
		}
	}


    public function indexAction()
    {
		$request = $this->getRequest();
		$form    = new Admin_Form_General();

		// Check to see if this action has been POST'ed to.
		if ($request->isPost()) {

			// Now check to see if the form submitted exists, and
			// if the values passed in are valid for this form.
			if ($form->isValid($request->getPost())) {

				$values = $form->getValues();

				$optsTable = new Euso_Model_DbTable_Options();

				$dbData = array('option_value' => $values['sitename']);
				$updRows = $optsTable->update($dbData, array('section= ?' => 'site', 'key= ?' => 'name'));
				if ($updRows != 1) {
					$dbData['section'] = 'site';
					$dbData['option_key'] = 'name';
					$dbData['autoload'] = '1';
					$updRows = $optsTable->insert($dbData);
				}
				$dbData = array('option_value' => $values['tagline']);
				$updRows = $optsTable->update($dbData, array('section= ?' => 'site', 'key= ?' => 'tagline'));
				if ($updRows != 1) {
					$dbData['section'] = 'site';
					$dbData['option_key'] = 'tagline';
					$dbData['autoload'] = '1';
					$updRows = $optsTable->insert($dbData);
				}
				$dbData = array('option_value' => $values['footer_text']);
				$updRows = $optsTable->update($dbData, array('section= ?' => 'site', 'key= ?' => 'footer.notice'));
				if ($updRows != 1) {
					$dbData['section'] = 'site';
					$dbData['option_key'] = 'footer.notice';
					$dbData['autoload'] = '1';
					$updRows = $optsTable->insert($dbData);
				}
				$dbData = array('option_value' => $values['admin_email']);
				$updRows = $optsTable->update($dbData, array('section= ?' => 'site', 'key= ?' => 'administrator.email'));
				if ($updRows != 1) {
					$dbData['section'] = 'site';
					$dbData['option_key'] = 'administrator.email';
					$dbData['autoload'] = '1';
					$updRows = $optsTable->insert($dbData);
				}

				// Refresh
				$this->_redirect($request->getPathInfo());
			}

		} else {

			$gOpts = Kernel::getInstance()->getOptions();

			$sitename = $form->getElement('sitename');
			$sitename->setValue(Kernel::getInstance()->getSiteName());
			$tagline = $form->getElement('tagline');
			$tagline->setValue(Kernel::getInstance()->getSiteTagline());
			$footerText = $form->getElement('footer_text');
			$footerText->setValue(Kernel::getInstance()->getSiteFooterNotice());
			$adminEmail = $form->getElement('admin_email');
			$adminEmail->setValue($gOpts['site']['administrator']['email']);

		}


		if ($this->_getParam('fmsg')) {
			$flashMessenger = $this->_helper->FlashMessenger;
			$flashMessenger->setNamespace($this->_getParam('fmsg'));
			$this->view->flashMessages = $flashMessenger->getMessages();
		}
		if (is_array($flashMessages)) {
			if (is_array($this->view->flashMessages)) {
				$this->view->flashMessages = array_unique(array_merge($this->view->flashMessages, $flashMessages));
			} else {
				$this->view->flashMessages = $flashMessages;
			}
		}

		$this->view->form = $form;

		Kernel::getInstance()->setHeadTitle('Admin: General Settings');

		$this->view->singleWidgetPageTitle = 'General Settings';
		$this->view->singleWidgetPage = TRUE;
	}
}
